import threading
import time


class MainThread:
    def __init__(self):
        self.SqTime = .2
        self.__BASEPATH = './thread_source/over_validate/'
        self.overFile = "Any"

    def overValidate(self) -> bool:
        try:
            with open(self.__BASEPATH + self.overFile, 'r') as f:
                return f.read() == "OVERTHREAD"
        except FileNotFoundError as e:
            print("线程资源新建立中")
            with open(self.__BASEPATH + self.overFile, 'w') as f:
                f.write('HelloWorld')
            return self.overValidate()

    def close(self):
        with open(self.__BASEPATH + self.overFile, 'w') as f:
            f.write('OVERTHREAD')

    def threadBody(self):
        ...

    def Validate(self) -> bool:
        return True

    def __thread(self):
        while not self.overValidate():
            if self.Validate():
                self.threadBody()
            time.sleep(self.SqTime)

    def openThread(self):
        print("【线程对象】 打开了")
        self.__t = threading.Thread(target=self.__thread)
        self.__t.daemon = True  # 默认是守护线程
        self.__t.start()
